//
//  WbStatuesRetweetView.swift
//  Weibo
//
//  Created by HH on 16/12/5.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SnapKit

class WbStatuesRetweetView: WbBaseView {
    
    var bottomConstraint:Constraint?
    
    var pictureViewModel:WbPictureViewModel = WbPictureViewModel.sharedPictureViewModel
    
    var statuesViewModel:WbStatusesViewModel?{
        
        didSet{
            
           
            
            contentLabel.text = statuesViewModel?.retwetStatusesContent
            
            //当有无picture时改变约束
            //传值给ViewModel处理数据
            pictureViewModel.pic_Url = statuesViewModel?.statuses?.retweeted_status?.pic_urls
            //传值给View展示数据
            pictureView.pic_url = statuesViewModel?.statuses?.retweeted_status?.pic_urls
            
            bottomConstraint?.deactivate()
            
            if let picUrl = statuesViewModel?.statuses?.retweeted_status?.pic_urls,picUrl.count > 0 {
                
                pictureView.isHidden = false
                
                self.snp.makeConstraints({ (make) in
                   bottomConstraint = make.bottom.equalTo(pictureView).offset(marign).constraint
                })
                
//               pictureView.snp.updateConstraints({ (make) in
//                make.size.equalTo(pictureViewModel.pictureViewSize!)
//                
//               })
                
            }else{
                
                pictureView.isHidden = true
                
                self.snp.makeConstraints({ (make) in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(marign).constraint
                })
                
//                pictureView.snp.updateConstraints({ (make) in
//                    make.size.equalTo(CGSize.zero)
//                })
//
            }
            
            //不知道为什么,加上去就好用了
//            self.layoutIfNeeded()
        }
    }
    
    //转发微博内容
    lazy private var contentLabel:UILabel = {
        
        let label = UILabel(text: "", font: 14, textColor: UIColor.darkGray)
        
        label.numberOfLines = 0
        
        return label
        
    }()
    
    lazy private var pictureView:WbStatusesPictureView = {
        
       
        let collectionView = WbStatusesPictureView()
        
        return collectionView
        
    }()

    override func setupUI() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        //转发微博内容
        contentLabel.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(marign)
            make.right.equalToSuperview().offset(-marign)
            
        }
        //配图
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(marign)
            make.left.equalTo(contentLabel)
            make.size.equalTo(CGSize.zero)
        }
        
        //关键约束
        self.snp.makeConstraints { (make) in
           bottomConstraint = make.bottom.equalTo(pictureView).offset(marign).constraint
        }
        
        
    }

}
